package com.itheima.pinda.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.common.utils.Result;
import com.itheima.pinda.entity.AddressBook;
import com.itheima.pinda.entity.Member;
import com.itheima.pinda.service.PdMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户信息管理")
@RestController
@RequestMapping(value = "/user/member")
public class PdMemberController {

    @Autowired
    private PdMemberService memberService;

    @ApiOperation(value = "用户新增")
    @PostMapping(value = "/Add")
    public Member insert(Member member){

        return memberService.insert(member);
    }

    @ApiOperation(value = "根据id查询用户详情")
    @GetMapping(value = "/selectById")
    public Member selectById(@RequestParam("id") String id) {
        return memberService.selectById(id);
    }


    @ApiOperation(value = "分页查询")
    @PostMapping(value = "/selectByPage")
    public PageResponse selectByPage(@RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize){
        IPage page = memberService.selectByPage(pageNum, pageSize);

        return PageResponse.builder()
                .page(pageNum)
                .pagesize(pageSize)
                .pages(page.getPages())
                .counts(page.getSize())
                .items(page.getRecords())
                .build();
    }

    @ApiOperation(value = "用户更新")
    @PostMapping(value = "/update")
    public Member update(Member member){

        return memberService.insert(member);
    }

    @ApiOperation(value = "用户删除")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestBody() List<String> ids){
        memberService.delete(ids);
        return Result.ok("删除成功！");
    }
}
